﻿using System.Windows;
using System.Windows.Input;
using Innvoice.Logica.GestorDB;
using Innvoice.Logica.GestorObject.Articulo;
using System.ComponentModel;

namespace Innvoice.Ws.Articulos.Categorias
{
    /// <summary>
    /// Lógica de interacción para ModificarCategoria.xaml
    /// </summary>
    public partial class ModificarCategoria : Window
    {
        #region Property

        private CategoriaArticulo _categoria = new CategoriaArticulo();
        private ArticuloDB articuloDb = new ArticuloDB();

        #endregion

        #region Constructor

        public ModificarCategoria()
        {
            InitializeComponent();
        }

        public ModificarCategoria(CategoriaArticulo categoria)
        {
            InitializeComponent();
            _categoria = categoria;
            DataContext = _categoria;
            _categoria.PropertyChanged += new PropertyChangedEventHandler(categoria_PropertyChanged);
            TextNombre.Focus();
        }

        #endregion

        #region Windows Methods

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();

        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;

            }
        }

        private void categoria_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = _categoria.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            articuloDb.UpdateCategoriaArticulo(_categoria, true);
            this.Close();
        }

        #endregion
    }
}
